/**
 * 
 */
package cn.idanda.core.service.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.idanda.core.dao.UserDao;
import cn.idanda.core.entity.oa.User;

/**
 * @author rain
 * @since 2015年9月20日 上午1:35:40
 */
@Service
public class UserManager {
	@Autowired
	private UserDao uDao;
	
	public User findByUsername(String username){
		return null;
	}
	
	/**
	 * 新建用户，并加密密码
	 * @param user
	 * @return
	 */
	public User createUser(User user) {  
	    //加密密码  
//	    encryptPassword(user);  
	    return uDao.save(user);  
	}  
	
	public List<Map<String,Object>> getAllUser(){
		List<Map<String,Object>> userinfo = new ArrayList<Map<String,Object>>();
		List<User> users =  (List<User>) uDao.findAll();
		for(User user : users){
			Map<String,Object> u = new HashMap<String,Object>();
			u.put("name", user.getUsername());
			u.put("password", user.getPassword());
			u.put("time", user.getCreateTime());
			userinfo.add(u);
		}
		return userinfo;
	}
	
	
	public Map<String,Object> getUserName(String name){
		Map<String,Object> userinfo = new HashMap<String,Object>();
		User user = uDao.findByUsername(name);
		userinfo.put("name",user.getUsername());
		userinfo.put("password", user.getPassword());
		return userinfo;
	}
	
//	public void changePassword(Long userId, String newPassword) {  
//	    User user =uDao.findOne(userId);
//	    user.setPassword(newPassword);  
//	    encryptPassword(user);  
//	    uDao.save(user);  
//	}  
//	
//	public Set<String> findRoles(String username){
//		return null;
//	}
//	
//	public Set<String> findPermissions(String username){
//		return null;
//	}
//	
//	private void encryptPassword(User user){
//		
//	}
//	
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
//        String username = (String)principals.getPrimaryPrincipal();  
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();  
//        authorizationInfo.setRoles(findRoles(username));  
//        authorizationInfo.setStringPermissions(findPermissions(username));  
//        return authorizationInfo;  
//    }  
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {  
//        String username = (String)token.getPrincipal();  
//        User user = findByUsername(username);  
//        if(user == null) {  
//            throw new UnknownAccountException();//没找到帐号  
//        }  
//        if(Boolean.TRUE.equals(user.isLocked())) {  
//            throw new LockedAccountException(); //帐号锁定  
//        }  
//        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配，如果觉得人家的不好可以在此判断或自定义实现  
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(  
//                user.getUsername(), //用户名  
//                user.getPassword(), //密码  
//                ByteSource.Util.bytes(user.getSalt()),//salt=username+salt  
//                getName()  //realm name  
//        );  
//        return authenticationInfo;  
//    }  
}
